#!/usr/bin/env Rscript

rm(list = ls())
options(stringsAsFactors=FALSE)

suppressMessages(library(dplyr))

setwd("")

cat("\nReading..\n")
## INDIV <- read.csv(
##     "formattedByIndividual_CaliAllYears_shorterFormatted.csv.gz"
## )
names(INDIV) <- gsub("cali3","cali",names(INDIV))

names(INDIV) <- c(names(INDIV)[-2],"extra")

formatYob <- function(x, c1, c2) {
    x <- as.integer(substr(x, c1, c2))
    x[x<1901|x>2000] <- NA
    return(x)
    }

cat("Formatting (1)..\n")
INDIV$yob07 <- formatYob(INDIV$dateofbirth_cali07, 1, 4)
INDIV$yob09 <- formatYob(INDIV$dateofbirth_cali09, 1, 4)
INDIV$yob10 <- formatYob(INDIV$dateofbirth_cali10, 1, 4)
INDIV$yob12 <- formatYob(INDIV$dateofbirth_cali12, 1, 4)
INDIV$yob14 <- formatYob(INDIV$dateofbirth_cali14, 1, 4)

countPartisans <- function(year="07") {
    eval(parse(text = paste("
    thesummary <- INDIV %>%
        summarise(
            dem=sum(party_cali",year,"==\"dem\"),
            rep=sum(party_cali",year,"==\"rep\"),
            dsnpp=sum(party_cali",year,"%in%c(\"ds\",\"npp\"))
        )
",
                   sep=""
                            )
               )
         )
    return(thesummary)
}

partisans07 <- countPartisans()
partisans09 <- countPartisans("09")
partisans10 <- countPartisans("10")
partisans12 <- countPartisans("12")
partisans14 <- countPartisans("14")

partySwitchSummary <- function(
    x=list(c("07","09"),c("09","10"),c("10","12"),c("12","14")),
    xMove=list(c("07","09"),c("09","10"),c("10","12"),c("12","14")),
    extrafilter="",
    mover=FALSE,
    voter=FALSE,
    inPeriodMover,
    lastvote=list(
        "07"="pg08_cali09",
        "09"="gg10_cali10",
        "10"="pg12_cali12",
        "12"="gg14_cali14"
    )) {
    return(
    lapply(1:4, function(i) {
        eval(
            parse(
                text=paste("INDIV %>%
    filter(
        !is.na(yob07)
        & !is.na(yob09)
        & !is.na(yob10)
        & !is.na(yob12)
        & !is.na(yob14)
        ",ifelse(
            inPeriodMover,
            paste("& mover",x[[i]][1],"to",x[[i]][2],"==1",sep=""),
            paste("& mover",x[[i]][1],"to",x[[i]][2],"==0",sep="")
        ),
                    extrafilter,
                    ifelse(
                        voter,
                        paste("& ",lastvote[[x[[i]][1]]],"==1 "),
                        ""
                    ),
                    ifelse(
                        mover,
                        paste(
                            "& mover",xMove[[i]][1],"to",xMove[[i]][2],"==1",
                            sep=""),
                        ""
                    ),"
    ) %>%
        group_by(yob",x[[i]][1],") %>%
            summarise(
                majorparty=sum(
                    party_cali",x[[i]][1]," %in% c(\"dem\",\"rep\")
                    & party_cali",x[[i]][2]," %in% c(\"dem\",\"rep\")
                ),
                changeparty=sum(
                    party_cali",x[[i]][1],"!=party_cali",x[[i]][2],"
                    & party_cali",x[[i]][1]," %in% c(\"dem\",\"rep\")
                    & party_cali",x[[i]][2]," %in% c(\"dem\",\"rep\")
                ),
                endindependent=sum(
                    party_cali",x[[i]][1]," != \"\"
                    & party_cali",x[[i]][2]," != \"\"
                    & party_cali",x[[i]][2]," %in% c(\"ds\",\"npp\")
                ),
                toindependent=sum(
                    party_cali",x[[i]][1]," != \"\"
                    & party_cali",x[[i]][2]," != \"\"
                    & !(party_cali",x[[i]][1]," %in% c(\"ds\", \"npp\"))
                    & party_cali",x[[i]][2]," %in% c(\"ds\",\"npp\")
                ),
                startindependent=sum(
                    party_cali",x[[i]][1]," != \"\"
                    & party_cali",x[[i]][2]," != \"\"
                    & party_cali",x[[i]][1]," %in% c(\"ds\", \"npp\")
                ),
                fromindependent=sum(
                    party_cali",x[[i]][1]," != \"\"
                    & party_cali",x[[i]][2]," != \"\"
                    & party_cali",x[[i]][1]," %in% c(\"ds\", \"npp\")
                    & !(party_cali",x[[i]][2]," %in% c(\"ds\",\"npp\"))
                ),
                count=n(),
                movers=sum(mover",xMove[[i]][1],"to",xMove[[i]][2],"),
                majorpartydemstart=sum(
                    party_cali",x[[i]][1]," %in% c(\"dem\")
                    & party_cali",x[[i]][2]," %in% c(\"dem\",\"rep\")
                ),
                majorpartydemtorep=sum(
                    party_cali",x[[i]][1]," %in% c(\"dem\")
                    & party_cali",x[[i]][2]," %in% c(\"rep\")
                ),
                majorpartyrepstart=sum(
                    party_cali",x[[i]][1]," %in% c(\"rep\")
                    & party_cali",x[[i]][2]," %in% c(\"dem\",\"rep\")
                ),
                majorpartyreptodem=sum(
                    party_cali",x[[i]][1]," %in% c(\"rep\")
                    & party_cali",x[[i]][2]," %in% c(\"dem\")
                )
                )", sep = ""
                           )
                )
        )
        }
        )
    )
}

cat("Summarizing..\n")
i <- 0
pb <- txtProgressBar(style=3)
INDIV.partySummary.movers <- partySwitchSummary(
    mover=TRUE,
    inPeriodMover=FALSE
)
i <- i+1/12; setTxtProgressBar(pb, i)
INDIV.partySummary.nevermoved <- partySwitchSummary(
    extrafilter="& mover07to09==0 & mover09to10==0 & mover10to12==0 & mover12to14==0",
    inPeriodMover=FALSE
)
i <- i+1/12; setTxtProgressBar(pb, i)
INDIV.partySummary.movers.before <- partySwitchSummary(
    mover=TRUE,
    inPeriodMover=FALSE,
    xMove=list(c("07","09"),c("07","09"),c("09","10"),c("10","12"))
)
i <- i+1/12; setTxtProgressBar(pb, i)
INDIV.partySummary.movers.before2 <- partySwitchSummary(
    mover=TRUE,
    inPeriodMover=FALSE,
    xMove=list(c("07","09"),c("07","09"),c("07","09"),c("09","10"))
)
i <- i+1/12; setTxtProgressBar(pb, i)
INDIV.partySummary.movers.after <- partySwitchSummary(
    mover=TRUE,
    inPeriodMover=FALSE,
    xMove=list(c("09","10"),c("10","12"),c("12","14"),c("07","09"))
)
i <- i+1/12; setTxtProgressBar(pb, i)
INDIV.partySummary.movers.after2 <- partySwitchSummary(
    mover=TRUE,
    inPeriodMover=FALSE,
    xMove=list(c("10","12"),c("12","14"),c("07","09"),c("07","09"))
)
i <- i+1/12; setTxtProgressBar(pb, i)
INDIV.partySummary.movers.voters <- partySwitchSummary(
    voter=TRUE,mover=TRUE,
    inPeriodMover=FALSE
)
i <- i+1/12; setTxtProgressBar(pb, i)
INDIV.partySummary.voters.nevermoved <- partySwitchSummary(
    voter=TRUE,
    inPeriodMover=FALSE,
    extrafilter="& mover07to09==0 & mover09to10==0 & mover10to12==0 & mover12to14==0"
)
i <- i+1/12; setTxtProgressBar(pb, i)
INDIV.partySummary.movers.voters <- partySwitchSummary(
    mover=TRUE,voter=TRUE,
    inPeriodMover=FALSE
)
i <- i+1/12; setTxtProgressBar(pb, i)
INDIV.partySummary.movers.voters.before <- partySwitchSummary(
    mover=TRUE,voter=TRUE,
    inPeriodMover=FALSE,
    xMove=list(c("07","09"),c("07","09"),c("09","10"),c("10","12"))
)
i <- i+1/12; setTxtProgressBar(pb, i)
INDIV.partySummary.movers.voters.before2 <- partySwitchSummary(
    mover=TRUE,voter=TRUE,
    inPeriodMover=FALSE,
    xMove=list(c("07","09"),c("07","09"),c("07","09"),c("09","10"))
)
i <- i+1/12; setTxtProgressBar(pb, i)
INDIV.partySummary.movers.voters.after <- partySwitchSummary(
    mover=TRUE,voter=TRUE,
    inPeriodMover=FALSE,
    xMove=list(c("09","10"),c("10","12"),c("12","14"),c("07","09"))
)
i <- i+1/12; setTxtProgressBar(pb, i)
INDIV.partySummary.movers.voters.after2 <- partySwitchSummary(
    mover=TRUE,voter=TRUE,
    inPeriodMover=FALSE,
    xMove=list(c("10","12"),c("12","14"),c("07","09"),c("07","09"))
)
i <- i+1/12; setTxtProgressBar(pb, i)
close(pb)

## change inPeriodMover=FALSE to TRUE for movers
cat("Writing..\n")
save(
    INDIV.partySummary.movers,
    INDIV.partySummary.nevermoved,
    INDIV.partySummary.movers.before,
    INDIV.partySummary.movers.after,
    INDIV.partySummary.movers.before2,
    INDIV.partySummary.movers.after2,
    INDIV.partySummary.movers.voters,
    INDIV.partySummary.voters.nevermoved,
    INDIV.partySummary.movers.voters.before,
    INDIV.partySummary.movers.voters.after,
    INDIV.partySummary.movers.voters.before2,
    INDIV.partySummary.movers.voters.after2,
    file="individual_partychanges_summary_beforeaftermoves_nonmovers.RData"
)

cat("\nDone (EOF).\n")
